package com.edu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.demo.entity.Contract;
import com.edu.demo.entity.ProResume;
import com.edu.demo.exception.BadCreditException;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IContractService;
import com.edu.demo.service.IProResumeService;
import com.edu.demo.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * 合同表 前端控制器
 * </p>
 *
 * @author jixuewei
 * @since 2023-01-12
 */
@RestController
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    private IContractService iContractService;

    @Autowired
    IProResumeService proResumeService;

    /**
     * 添加合同：教师签订某个学生
     * @param contract
     * @return
     */
    @PostMapping("/submit")
    @PreAuthorize("@ps.hasPerm('tea:contract:insert')")
    public AjaxResult contract(@RequestBody Contract contract){
        try {

            QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
            List<Contract> contracts = iContractService.list(queryWrapper.eq("stu_id", SecurityUtils.getStudent().getId()).eq("project_id",contract.getProjectId()));
            if(contracts.isEmpty())
                 iContractService.addContract(contract);
            else return AjaxResult.error("该项目与学生合同重复!");

            return AjaxResult.success();
        }catch (BadCreditException e){
            return AjaxResult.error(e.getMessage());
        }

    }


    /**
     * 教师查看项目已经签订的合同
     */
    @GetMapping("/list/for/project")
    @PreAuthorize("@ps.hasPerm('tea:contract:list')")
    public AjaxResult list(@RequestParam Integer projectId){
        Map<String, Object> list = iContractService.list(projectId);
        return AjaxResult.success(list);
    }

    /**
     * 教师查看待支付的合同
     */
    @GetMapping("/list/for/pay")
    @PreAuthorize("@ps.hasPerm('tea:contract:list')")
    public AjaxResult list(@RequestParam int state){
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",state);
        List<Contract> list = iContractService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    /**
     * 学生查看自己的合同
     * @param
     * @return
     */
    @GetMapping("/of/student")
    @PreAuthorize("@ps.hasPerm('stu:contract:query')")
    public AjaxResult contractOfStudent(){
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        List<Contract> contract = iContractService.list(queryWrapper.eq("stu_id", SecurityUtils.getStudent().getId()));

        return AjaxResult.success(contract);
    }


    /**
     * 学生更新自己合同状态
     * @return
     */
    @GetMapping("/upd/contract")
    @PreAuthorize("@ps.hasPerm('stu:contract:query')")
    public AjaxResult updateContractByStudent(@RequestParam int pId,@RequestParam int status){
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        List<Contract> contract = iContractService.list(queryWrapper.eq("stu_id", SecurityUtils.getStudent().getId()).eq("project_id",pId));

        for (Contract contract1 : contract) {
            if(contract1.getStatus()==5) {
                contract1.setStatus((byte)status);
                iContractService.updateById(contract1);

                if(status==-1) { //学生选择放弃
                    ProResume entity = new ProResume();
                    entity.setProjectId(pId);
                    entity.setStuId(SecurityUtils.getStudent().getId());
                    entity.setStatus(5);
                    proResumeService.updateById(entity);
                }
            }
        }
        return AjaxResult.success(contract);
    }



    /**
     * 教师上传支付酬劳证明
     * @param contract
     * @return
     */
    @PostMapping("/pay")
    @PreAuthorize("@ps.hasPerm('tea:pay:update')")
    public AjaxResult pay(@RequestBody Contract contract){
        if(contract.getStatus()!=3){
            contract.setStatus((byte) 2);
        }
        boolean update = iContractService.update(contract, new QueryWrapper<Contract>()
                .eq("project_id", contract.getProjectId())
                .eq("stu_id", contract.getStuId()));
        if(update)
        return AjaxResult.success();
        return AjaxResult.error();
    }


}
